//
//  CollectionBottomView.m
//  Yanwei
//
//  Created by 刘文 on 2017/11/30.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "CollectionBottomView.h"

@interface CollectionBottomView ()

/** 全选 */
@property (strong, nonatomic) UIButton *selectAllButton;
/** 删除 */
@property (strong, nonatomic) UIButton *deleteButton;

@end

@implementation CollectionBottomView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.selectAllButton.hidden = NO;
        self.deleteButton.hidden = NO;
    }
    return self;
}

- (void)setSelectedAll:(BOOL)selectedAll {
    _selectedAll = selectedAll;
    
    self.selectAllButton.selected = selectedAll;
    [self.selectAllButton layoutIfNeeded];
    [self.selectAllButton setNeedsLayout];
}

- (void)leftButtonAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if (self.clickedSelectedActionBlock) {
        self.clickedSelectedActionBlock(sender.selected);
    }
}

- (void)rightButtonAction {
    if (self.clickedDeleteActionBlock) {
        self.clickedDeleteActionBlock();
    }
}

#pragma mark -
- (UIButton *)selectAllButton {
    if (!_selectAllButton) {
        _selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectAllButton setTitle:@" 全选" forState:UIControlStateNormal];
        [_selectAllButton setImage:[UIImage imageNamed:@"dz_button_no"] forState:UIControlStateNormal];
        [_selectAllButton setTitle:@" 全选" forState:UIControlStateSelected];
        [_selectAllButton setImage:[UIImage imageNamed:@"dz_button_yes"] forState:UIControlStateSelected];
        [_selectAllButton setTitleColor:Color34 forState:UIControlStateNormal];
        [_selectAllButton setTitleColor:Color34 forState:UIControlStateSelected];
        _selectAllButton.titleLabel.font = APPFONT(14);
        [_selectAllButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_selectAllButton];
        [_selectAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.top.equalTo(self);
            make.bottom.equalTo(self);
            make.width.equalTo(self).dividedBy(2);
        }];
    }
    return _selectAllButton;
}

- (UIButton *)deleteButton {
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteButton setTitle:@"删除" forState:UIControlStateSelected];
        [_deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        _deleteButton.titleLabel.font = APPFONT(14);
        [_deleteButton setBackgroundColor:Color175030];
        [_deleteButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteButton];
        [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.selectAllButton.mas_right);
            make.top.equalTo(self.selectAllButton);
            make.bottom.equalTo(self.selectAllButton);
            make.width.equalTo(self.selectAllButton);
        }];
    }
    return _deleteButton;
}

@end
